package com.example.service.impl;

import com.example.mapper.UserRoleMapper;
import com.example.pojo.Result;
import com.example.pojo.UserRole;
import com.example.service.PermissionService;
import com.example.service.RoleCacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.example.constant.RoleConstant;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.awt.*;
import java.util.Collections;

@Service
@RequiredArgsConstructor
@Slf4j
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleCacheService roleCacheService;

    /**
     * 绑定用户默认角色
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public Result bindDefaultRole(Long userId) {
        if (userId == null){
            return Result.error("用户ID不能为空");
        }
        Long roleId = userRoleMapper.findRoleIdsByUserId(userId);
        if (roleId == null || roleId == 0 ) {
            // 绑定默认角色
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(RoleConstant.DEFAULT_ROLE_ID);
            userRoleMapper.insert(userRole);
            log.info("绑定用户ID:{} 默认角色成功", userId);
            // 清除用户权限缓存
            roleCacheService.evictUserRoles(userId);
        }
        return Result.success();
    }

    /**
     * 获取用户角色编码
     * @param userId
     * @return
     */
    @Override
    public String getUserRoleCode(Long userId) {
        if (userId == null){
            log.error("用户ID不能为空");
            return null;
        }
        Long roleId = userRoleMapper.findRoleIdByUserId(userId);
        if (roleId == null){
            log.error("用户ID:{} 未绑定角色", userId);
            return null;
        }
        String roleCode = userRoleMapper.findRoleCodeById(roleId);
        return roleCode;
    }

    /**
     * 超管调用：升级用户为管理员
     * @param userId
     * @return
     */
    @Override
    public void upgradeUser(Long userId) {
        if (userId == null){
            log.error("用户ID不能为空");
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "用户ID不能为空");
        }
        Integer count = userRoleMapper.upgradeUser(userId);
        if (count == 1){
            log.info("用户ID:{} 升级成功", userId);
            return;
        }
        log.info("用户ID:{} 升级失败", userId);
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "用户升级失败");

    }

    @Override
    public void downgradeUser(Long userId) {
        if (userId == null) {
            log.info("用户ID不能为空");
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "用户ID不能为空");
        }
        Integer count = userRoleMapper.downgradeUser(userId);
        if (count == 1){
            log.info("用户ID:{} 降级成功", userId);
            return;
        }
        log.info("用户ID:{} 降级失败", userId);
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "用户降级失败");
    }
}
